//
//  UIView+MZCategory.m
//  MZToolkitDemo
//
//  Created by 沈家林 on 2017/11/1.
//  Copyright © 2017年 Chris Yang. All rights reserved.
//

#import "UIView+MZCategory.h"
#import <objc/runtime.h>

static char mzActionHandlerTapBlockKey;
static char mzActionHandlerTapGestureKey;
static char mzkActionHandlerLongPressBlockKey;
static char mzkActionHandlerLongPressGestureKey;

@implementation UIView (MZCategory)

#pragma mark xib扩展

- (void)setMzBorderColor:(UIColor *)mzBorderColor{
    self.layer.borderColor = mzBorderColor.CGColor;
}

- (UIColor *)mzBorderColor{
    return [UIColor colorWithCGColor:self.layer.shadowColor];
}

- (void)setMzBorderWidth:(CGFloat)mzBorderWidth{
    self.layer.borderWidth = mzBorderWidth;
}

- (CGFloat)mzBorderWidth{
    return self.layer.borderWidth;
}

- (void)setMzCornerRadius:(CGFloat)mzCornerRadius{
    self.layer.cornerRadius = mzCornerRadius;
}

- (CGFloat)mzCornerRadius{
    return self.layer.cornerRadius;
}

- (void)setMzMasksToBounds:(BOOL)mzMasksToBounds{
    self.layer.masksToBounds = mzMasksToBounds;
}

- (BOOL)mzMasksToBounds{
    return self.layer.masksToBounds;
}


//设置阴影
- (void)setMzShadowColor:(UIColor *)mzShadowColor{
    self.layer.shadowColor = mzShadowColor.CGColor;
}

- (UIColor *)mzShadowColor{
    return [UIColor colorWithCGColor:self.layer.shadowColor];
}

- (void)setMzShadowOffset:(CGSize)mzShadowOffset{
    self.layer.shadowOffset = mzShadowOffset;
}

- (CGSize)mzShadowOffset{
    return self.layer.shadowOffset;
}

- (void)setMzShadowOpacity:(CGFloat)mzShadowOpacity{
    self.layer.shadowOpacity = mzShadowOpacity;
}

- (CGFloat)mzShadowOpacity{
    return self.layer.shadowOpacity;
}

- (void)setMzShadowRadius:(CGFloat)mzShadowRadius{
    self.layer.shadowRadius = mzShadowRadius;
}

- (CGFloat)mzShadowRadius{
    return self.layer.shadowRadius;
}


#pragma mark block类型手势

- (void)mzAddTapActionWithBlock:(MZGestureActionBlock)block{
    UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &mzActionHandlerTapGestureKey);
    if (!gesture){
        gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
        [self addGestureRecognizer:gesture];
        objc_setAssociatedObject(self, &mzActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    objc_setAssociatedObject(self, &mzActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
}
- (void)handleActionForTapGesture:(UITapGestureRecognizer*)gesture{
    if (gesture.state == UIGestureRecognizerStateRecognized){
        MZGestureActionBlock block = objc_getAssociatedObject(self, &mzActionHandlerTapBlockKey);
        if (block){
            block(gesture);
        }
    }
}

- (void)mzAddLongPressActionWithBlock:(MZGestureActionBlock)block{
    UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &mzkActionHandlerLongPressGestureKey);
    if (!gesture){
        gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
        [self addGestureRecognizer:gesture];
        objc_setAssociatedObject(self, &mzkActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    objc_setAssociatedObject(self, &mzkActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)handleActionForLongPressGesture:(UITapGestureRecognizer*)gesture{
    if (gesture.state == UIGestureRecognizerStateRecognized){
        MZGestureActionBlock block = objc_getAssociatedObject(self, &mzkActionHandlerLongPressBlockKey);
        if (block){
            block(gesture);
        }
    }
}

@end
